/**
 * Created by walkman on 2017/7/21.
 */

/**
 * @angular 管理员
 * @description
 * @auth yang li
 */

customer = function ($scope, $ducTools, $compile, $get, http, $dataTableSet) {
    'use strict';

    var customerTable = $('#customerTable').DataTable({
        sAjaxSource: 'customer/queryCustomer',
        fnServerData: $ducTools.retrieveData,
        fnServerParams: function (aoData) {
            aoData.push(
                {"name": "userCode", "value": $scope.userCode},
                {"name": "userPhone", "value": $scope.userPhone},
                {"name": "userName", "value": $scope.userName},
                {"name": "activeStatus", "value": $scope.activeStatus}//,
                // {"name": "registerStartTime", "value": $("#registerStartTime").val()},
                // {"name": "registerEndTime", "value": $("#registerEndTime").val()},
                // {"name": "activateStartTime", "value": $("#activateStartTime").val()},
                // {"name": "activateEndTime", "value": $("#activateEndTime").val()}
            )
        },
        columns: [
            {"data": "userId"},
            {"data": "userCode"},
            {"data": "userName"},
            {"data": "userPhone"},
            {"data": "userRegTime"},
            {"data": "userActStatus"},
            {"data": "userActTime"},
            {"data": "userLevel"},
            {"data": null},
        ],
        columnDefs: [
            {
                targets: 5,
                render: function (activeStatus) {
                    if (activeStatus == 1) {
                        return "未激活";
                    } else if (activeStatus == 2) {
                        return "已激活";
                    } else if (activeStatus == 3) {
                        return "已注销";
                    }
                }
            },
            {
                targets: 7,
                render: function (userLevel) {
                    if (userLevel == 0) {
                        return "普通用户";
                    } else if (userLevel == 1) {
                        return "经理";
                    } else if (userLevel == 2) {
                        return "总监";
                    } else if (userLevel == 3) {
                        return "总裁";
                    }
                }
            },
            {
                targets: 8,
                render: function (c) {

                    var restr = "";
                    if (c.userActStatus == "1") {
                        //未激活
                        restr = "<button class='btn btn-success btn-xs' ng-click=showUserDetail('" + c.userId + "')> 查看/编辑详细信息 </button> &nbsp;" +
                            "<button class='btn btn-info btn-xs' ng-click=activateCustomer('" + c.userId + "')> 激活 </button>";
                    } else {
                        //已激活
                        restr = "<button class='btn btn-success btn-xs' ng-click=showUserDetail('" + c.userId + "')> 查看/编辑详细信息 </button> &nbsp;" +
                            "<button class='btn btn-info btn-xs' ng-click=showTransferPoint('" + c.userId + "')> 积分划拨 </button> &nbsp;";
                    }
                    return restr + "<button class='btn btn-info btn-xs' ng-click=showPwdReset('" + c.userId + "')> 密码重置 </button>";
                }
            },
            {
                "targets": 0,
                "visible": false
            }
        ],
        createdRow: function (row, data, dataIndex) {
            $compile(row)($scope);
        }
    });

    $dataTableSet.dataTableRowShow('customerTable');

    $scope.customerRecordDraw = function () {
        customerTable.draw();
    };

    //激活
    $scope.activateCustomer = function (userId) {
        swal({
            title: '确定要激活该用户吗？',
            showCancelButton: true,
            closeOnConfirm: false,
            confirmButtonText: "确定",
            confirmButtonColor: "#046dae",
            cancelButtonText: "取消",
            animation: "slide-from-top"
        }, function () {
            http.post(
                {
                    "userId": userId
                },
                'customer/activeCustomer').then(function (data) {
                swal("成功！");
                customerTable.draw();
            });
        });
    };

    //划拨积分
    $scope.showTransferPoint = function (userId) {

        $("#showTransferModal").modal('show');
        $scope.transferDetail = {};
        //详情
        http.post({"uuid": userId}, "customer/userinfo").then(function (data) {

            $scope.transferDetail =
                {
                    cpUser: data.tid,
                    cpUserCode: data.userCode
                };
        });
    };

    //密码重置
    $scope.showPwdReset = function (userId) {

        $("#showPwdResetModal").modal('show');

        $scope.transferDetail = {};
        //详情
        http.post({"uuid": userId}, "customer/userinfo").then(function (data) {

            $scope.transferDetail =
                {
                    cpUser: data.tid,
                    cpUserCode: data.userCode
                };
        });
    }

    $scope.pwdReset = function () {

        http.post({
            "userId": $scope.transferDetail.cpUser,
            "pwdType": $("#resetType").val()
        }, "customer/pwdreset").then(function (data) {

            $("#showPwdResetModal").modal('hide');
            swal("操作成功");

        }, "json");
    }


    //划拨积分
    $scope.transferPoint = function (userId) {

        http.post({
            "cpUser": userId,
            "cpUserCode": $scope.transferDetail.cpUserCode,
            "cpPointAmount": $scope.transferDetail.cpPointAmount,
            "pointType": $scope.transferDetail.pointType,
        }, "customer/sysTranPoint").then(function (data) {

            $("#showTransferModal").modal('hide');
            swal("划拨成功！");
        }, "json");
    };

    //详情
    $scope.showUserDetail = function (userId) {

        $("#showUserDetail").modal('show');

        $scope.userDetail = {};

        http.post({"uuid": userId}, "customer/userinfo").then(function (data) {
            $scope.userDetail = angular.copy(data);
        });
    };

    $scope.getUserActiveStatus = function (activeStatus) {
        if (activeStatus == 1) {
            return "未激活";
        } else if (activeStatus == 2) {
            return "已激活";
        } else if (activeStatus == 3) {
            return "已注销";
        }
    };

    $scope.getUserLevel = function (userLevel) {
        if (userLevel == 0) {
            return "普通用户";
        } else if (userLevel == 1) {
            return "经理";
        } else if (userLevel == 2) {
            return "总监";
        } else if (userLevel == 3) {
            return "总裁";
        }
    };

    $scope.getUserIdAuth = function (userIdAuth) {
        if (userIdAuth == 2) {
            return "已认证";
        } else {
            return "未认证";
        }
    };

    $scope.getPacketBound = function (packetBound) {
        if (packetBound == 2) {
            return "已绑定";
        } else {
            return "未绑定";
        }
    };

    $scope.getUserTradepwdSet = function (tradepwdSet) {
        if (tradepwdSet == 2) {
            return "已设置";
        } else {
            return "未设置";
        }
    };

    $scope.updateUserDetail = function (userId) {

        $("#showUserDetail").modal('hide');

        // http.post(JSON.stringify({
        //     "filmCode": userId,
        //     "filmType": $("#filmType").val(),
        //     "showTime": $("#showTime").val(),
        //     "country": $("#country").val(),
        //     "releaseCompany": $("#releaseCompany").val(),
        //     "filmScore": $("#filmScore").val(),
        //     "introduction": $("#introduction").val(),
        //     "director": $("#director").val(),
        //     "directorPicture": $("#imgUpload1").attr("src"),
        //     "filmActorsViewList": actorList,
        //     "filmPostsViewList": stageList,
        //     "poster": $("#poster").attr("src"),
        //     "bigPoster": $("#bigPoster").attr("src"),
        // }), "basic/save_film_details", "application/json").then(function (data) {
        //
        //     swal("保存成功！");
        //     $("#actorList tr.patTr").remove();
        //     $("#stageMain .addNew").remove();
        // }, "json");
    };

    //图片上传
};

